package graph.day02;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Kama_101 {

    static int[][] graph;

    static Queue<Integer> queue = new LinkedList<>();

    static int[][] direction = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

    static int count = 0;


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        graph = new int[n][m];

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                graph[i][j] = sc.nextInt();
            }
        }

        for (int i = 0; i < n; i++) {
            if (graph[i][0] == 1) dfs(i, 0);
            if (graph[i][m - 1] == 1) dfs(i, m - 1);
        }
        for (int j = 0; j < m; j++) {
            if (graph[0][j] == 1) dfs(0, j);
            if (graph[n - 1][j] == 1) dfs(n - 1, j);
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (graph[i][j] == 1) count++;
            }
        }
        System.out.println(count);
    }

    private static void dfs(int i, int j) {
        graph[i][j] = 0;
        for (int[] dir : direction) {
            int x = i + dir[0];
            int y = j + dir[1];
            if (x < 0 || x >= graph.length || y < 0 || y >= graph[0].length) continue;
            if (graph[x][y] == 1) {
                dfs(x, y);
            }
        }
    }
}